<?php
namespace App\Admin\Models;
use Common\BaseModel;
class Withdraw extends BaseModel
{
    protected $res;
    public $key;
    public function getSource()
    {
        return "f_withdrawals";
    }

    public function getWithdrawlist($params)
    {
        $page = isset($params['page']) ? $params['page'] : 1;
        $size = isset($params['limit']) ? $params['limit'] : 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }

        $conditions = '';
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $conditions = " WHERE (Member.phone like '%{$params['keywords']}%' OR Member.nickname like '%{$params['keywords']}%')";
        }
        if ((isset($params['start_time']) && !empty($params['start_time'])) || (isset($params['end_time']) && !empty($params['end_time']))) {
            if ($conditions) {
                $conditions .= " AND (Withdraw.created_at >= '{$params['start_time']}' AND Withdraw.created_at <= '{$params['end_time']}')";
            } else {
                $conditions .= " WHERE (Withdraw.created_at >= '{$params['start_time']}' AND Withdraw.created_at <= '{$params['end_time']}')";
            }
        }
        if (isset($params['status']) && !empty($params['status'])) {
            if ($conditions) {
                $conditions .= " AND Withdraw.status={$params['status']}";
            } else {
                $conditions .= " WHERE Withdraw.status={$params['status']}";
            }
        }
        if (isset($params['user_ids']) && !empty($params['user_ids'])) {
            if ($conditions) {
                $conditions .= " AND Withdraw.user_id in({$params['user_ids']})";
            } else {
                $conditions .= " WHERE Withdraw.user_id in({$params['user_ids']})";
            }
        }
        $phql = "SELECT Withdraw.id,Withdraw.user_id,Withdraw.money,Withdraw.poundage,Withdraw.status,Withdraw.type,Withdraw.remark,Withdraw.tips,
                 Withdraw.created_at,Withdraw.is_payed,Withdraw.alipay_order_id,Withdraw.pay_date,Member.phone,Member.nickname,Member.avatar,Member.invite_code,Member.alipay_account,Member.real_name
                FROM f_withdrawals as Withdraw 
                LEFT JOIN f_users as Member ON Withdraw.user_id = Member.id
                {$conditions}
                ORDER BY Withdraw.id DESC limit {$page},{$size}";
        $phql = $this->getDI()->get('dbMaster')->fetchAll($phql);
        return $phql;
    }


    /**
     * @param $id根据提现id获取提现记录
    */
    public function find_by_id($id){
        $withdraw_info = $this->findFirstById($id)->toarray();
        if($withdraw_info){
            return $withdraw_info;
        }else{
            return [];
        }
    }


    public function money_totallist(){
        $conditions = "status in(1,2)";
        $money = $this->findFirst(array(
            $conditions,
            'columns'=>'sum(money) as money'
        ));
        $mo = $money->money ? $money->money : 0;
        return $mo;
    }

    public function money_total_count(){
        $money = $this->findFirst(array(
            'columns'=>'count(user_id) as count',
            'group' => 'user_id'
        ));
        $mo = $money->count ? $money->count : 0;
        return $mo;
    }

    public function money_total_yi_count(){
        $conditions = "status = 8";
        $money = $this->findFirst(array(
            $conditions,
            'columns'=>'sum(money) as money'
        ));
        $mo = $money->money ? $money->money : 0;
        return $mo;
    }

    public function save_withdraw($params){
        return $this::save($params);
    }

    public function user_money_totallist($user_id){
        $conditions = "user_id=$user_id and status in(1,2,8)";
        $money = $this->findFirst(array(
            $conditions,
            'columns'=>'sum(money) as money'
        ));
        $mo = $money->money  ? $money->money : '0.00';
        return $mo;
    }
    public function user_money_have_in_hand($user_id){
        $conditions = "user_id=$user_id and status in(1,2)";
        $money = $this->findFirst(array(
            $conditions,
            'columns'=>'sum(money) as money'
        ));
        $mo = $money->money  ? $money->money : '0.00';
        return $mo;
    }

    public function find_by_id_list($id)
    {
        $where['conditions'] = "id in ($id) and status=1";
        $withdraw_info = $this->find($where);
        if (!empty($withdraw_info->toarray())) {
            return $withdraw_info;
        } else {
            return [];
        }
    }
}
